﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityAndDomainLibrary.DomainModel;
using Log4NetLibrary;

namespace MeterServiceClientApp.UC.Config
{
    public partial class ucSystemSettingsConfig : UserControl
    {
        #region Private Variables
        ILogService logService;
        DataBaseAccess dbAcess = new DataBaseAccess();

        IList<Meterpoint> _listMeterPts;
        #endregion 
        
        public ucSystemSettingsConfig()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(ucSystemSettingsConfig));

                InitializeComponent();
                LoadData();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SystemSetting sys = new SystemSetting();
                sys.FileLocation = txtFileLocation.Text.Trim();

                sys.ArchiveLocation = txtArchiveLocation.Text.Trim();
                sys.OutputFileLocation = txtOp.Text.Trim();

                dbAcess.SaveSystemSettings(sys);
                lblMessage.Text = "Sucessfully Save!";
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
                lblMessage.Text = "Save Failed";
            }
               
        }

        private void LoadData()
        {
            try
            {
                SystemSetting sys = dbAcess.GetSystemSettings();
                if (sys != null)
                {
                    txtFileLocation.Text = sys.FileLocation;
                    txtArchiveLocation.Text = sys.ArchiveLocation;

                    txtOp.Text = sys.OutputFileLocation;
                }

                //log4net.Config.BasicConfigurator.Configure();
                //log4net.ILog log = log4net.LogManager.GetLogger(typeof(ucSystemSettingsConfig));
                //log.Debug("THis is sadi's world!");
                //log.Info("How beautyful the console looks like");
                //log.Warn("You are great you did this");
                //log.Error("Who make you know is the best");
                //log.Fatal("sadi the great");
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }
    }
}
